package com.ruoyi.system.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.domain.ReceiptFile;
import com.ruoyi.system.domain.ScriptRecordInfo;
import com.ruoyi.system.domain.vo.business.PageScriptRecordInfoVo;
import com.ruoyi.system.domain.vo.business.ReceiptFileVo;
import com.ruoyi.system.domain.vo.business.ScriptRecordInfoAlterVo;
import com.ruoyi.system.domain.vo.business.ScriptRecordInfoVo;
import com.ruoyi.system.domain.vo.coopHan.PageScriptRecordVo;
import com.ruoyi.system.dto.business.PageScriptRecordDto;
import com.ruoyi.system.dto.business.ReceiptDto;
import com.ruoyi.system.dto.coopHan.ScriptRecordDto;
import com.ruoyi.system.mapper.ScriptRecordInfoMapper;
import com.ruoyi.system.service.IReceiptFileService;
import com.ruoyi.system.service.IScriptRecordInfoService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 剧本备案信息管理 服务实现类
 * </p>
 *
 * @author yanfu
 * @since 2024-03-23
 */
@Service
@RequiredArgsConstructor
public class ScriptRecordInfoServiceImpl extends ServiceImpl<ScriptRecordInfoMapper, ScriptRecordInfo> implements IScriptRecordInfoService {

    private final IReceiptFileService receiptFileService;

    @Override
    public Page<PageScriptRecordInfoVo> getScriptRecordInfo(PageScriptRecordDto dto) {
        Page<ScriptRecordInfo> voPage = new Page<>(dto.getPageNum(), dto.getPageSize());
        Page<ScriptRecordInfo> page = baseMapper.getScriptRecordInfo(dto, voPage);
        Page<PageScriptRecordInfoVo> voPage1 = new Page<>(dto.getPageNum(), dto.getPageSize());
        voPage1.setTotal(page.getTotal());
        List<ScriptRecordInfo> records = page.getRecords();
        List<PageScriptRecordInfoVo> scriptRecordInfoVos = new ArrayList<>();
        for (ScriptRecordInfo record : records) {
            PageScriptRecordInfoVo scriptRecordInfoVo = new PageScriptRecordInfoVo();
            BeanUtils.copyProperties(record, scriptRecordInfoVo);
            List<ReceiptFile> list = receiptFileService.
                    list(Wrappers.lambdaQuery(ReceiptFile.class).
                            eq(ReceiptFile::getReceiptId, record.getId()));
            scriptRecordInfoVo.setFileNum(list.size());
            scriptRecordInfoVos.add(scriptRecordInfoVo);
        }
        voPage1.setRecords(scriptRecordInfoVos);
        return voPage1;
    }

    @Override
    public ScriptRecordInfoVo getScriptRecordInfodetail(Long id) {
        ScriptRecordInfoVo vo = new ScriptRecordInfoVo();
        ScriptRecordInfo recordInfo = getById(id);
        BeanUtils.copyProperties(recordInfo, vo);
        List<ReceiptFileVo> list = receiptFileService.getReceiptFile(id);
        vo.setReceiptFiles(list);
        String recordNum = recordInfo.getRecordNum();
        List<ScriptRecordInfo> recordInfos = list(Wrappers.lambdaQuery(ScriptRecordInfo.class).
                eq(ScriptRecordInfo::getRecordNum, recordNum).
                ne(ScriptRecordInfo::getId, id).
                orderByDesc(ScriptRecordInfo::getCreateTime));
        List<ScriptRecordInfoAlterVo> collect = recordInfos.stream().map(m -> {
            ScriptRecordInfoAlterVo scriptRecordInfoAlterVo = new ScriptRecordInfoAlterVo();
            BeanUtils.copyProperties(m, scriptRecordInfoAlterVo);
            return scriptRecordInfoAlterVo;
        }).collect(Collectors.toList());
        vo.setAlterVos(collect);
        return vo;
    }

    @Override
    public void saveReceipt(List<ReceiptDto> dtos) {
        List<ReceiptFile> files = dtos.stream().map(m -> {
            ReceiptFile file = new ReceiptFile();
            BeanUtils.copyProperties(m, file);
            return file;
        }).collect(Collectors.toList());
        receiptFileService.saveBatch(files);
    }

    @Override
    public Page<PageScriptRecordVo> scriptRecordPublicityInfo(ScriptRecordDto dto) {
        Page<PageScriptRecordVo> voPage = new Page<>(dto.getPageNum(), dto.getPageSize());
        voPage= baseMapper.scriptRecordPublicityInfo(dto, voPage);
        return voPage;
    }
}
